package com.doudou.collection.service.impl;

import com.doudou.collection.mapper.BlogMapper;
import com.doudou.collection.pojo.dto.BlogDTO;
import com.doudou.collection.pojo.entity.Blog;
import com.doudou.collection.pojo.vo.BlogCategoriesVO;
import com.doudou.collection.pojo.vo.BlogDetailVO;
import com.doudou.collection.pojo.vo.ContentIndexVO;
import com.doudou.collection.response.ResultVO;
import com.doudou.collection.service.BlogService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class BlogServiceImpl implements BlogService {

    @Autowired
    private BlogMapper blogMapper;


    /**
     * 根据分类id获取文章列表功能
     * @param categoriesId
     * @return
     */
    @Override
    public ResultVO queryAllArticle(@PathVariable Integer categoriesId) {
        log.debug("正在处理根据分类id获取文章功能,分类id为 {}", categoriesId);
        List<ContentIndexVO> list = blogMapper.selectByCategoriesId(categoriesId);

        log.info("主页文章查询成功");
        return ResultVO.ok(list);
    }




    /**
     * 发布文章功能
     * @param blogDTO
     * @return
     */
    @Override
    public ResultVO insert(BlogDTO blogDTO) {
        log.debug("用户发布的文章信息 {}", blogDTO);
        Blog blog = new Blog();
        BeanUtils.copyProperties(blogDTO, blog);

            blog.setCreateTime(new Date());
            blogMapper.insert(blog);
            log.info("用户发布文章成功");

        return ResultVO.ok();
    }






    /**
     * 根据id获取文章具体信息,每查询一次增加一个浏览量
     * @param id
     * @return
     */
    @Override
    public ResultVO detailById(@PathVariable Long id) {
        //通过id查询表示浏览了一次
        blogMapper.updateViewCountById(id);

        BlogDetailVO contentDetailVO = blogMapper.detailById(id);
        return ResultVO.ok(contentDetailVO);
    }


    /**
     * 获取文章的所有类别
     * @param
     * @return
     */
    @Override
    public ResultVO selectAllCategories() {
        List<BlogCategoriesVO> list = blogMapper.selectAllCategories();
        return ResultVO.ok(list);
    }

    /**
     * 删除文章
     * @param id
     * @return
     */
    @Override
    public ResultVO delete(@PathVariable Long id){
        blogMapper.delete(id);

        return ResultVO.ok();
    }






    /**
     * 查询用户发布的所有文章
     * @param id
     * @return
     */
    @Override
    public ResultVO selectSelfBlogsById(Long id) {

        List<ContentIndexVO> list = blogMapper.selectSelfBlogsById(id);

        log.info("查询成功");
        return ResultVO.ok(list);

    }

    /**
     * 查询用户点赞的所有文章
     * @param id
     * @return
     */
    @Override
    public ResultVO selectLikeBlogsAllById(Long id) {

        List<ContentIndexVO> list = blogMapper.selectLikeBlogsAllById(id);

        log.info("查询成功");
        return ResultVO.ok(list);
    }

    /**
     * 查询用户收藏的所有文章
     * @param id
     * @return
     */
    @Override
    public ResultVO selectCollectBlogsAllById(Long id) {

        List<ContentIndexVO> list = blogMapper.selectCollectBlogsAllById(id);

        log.info("查询成功");
        return ResultVO.ok(list);
    }


}
